package com.Cychat.doctor.mapper;

import com.Cychat.commons.entity.pojo.Doctor;
import com.Cychat.commons.entity.vo.DoctorVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DoctorMapper extends BaseMapper<Doctor> {

    /**
     * 原有接口：查询所有医生（联查科室、医院、用户昵称）
     */
    @Select("SELECT d.*, " +
            "dep.department_id as department_departmentId, " + // 别名对应Department的departmentId
            "h.hospital_id as hospital_hospitalId, " +         // 别名对应Hospital的hospitalId
            "u.nick_name as nick_name " +
            "FROM doctor d " +
            "LEFT JOIN department dep ON d.department_id = dep.department_id " +
            "LEFT JOIN hospital h ON dep.hospital_id = h.hospital_id " +
            "LEFT JOIN user_info u ON d.userinfo_id = u.userinfo_id")
    @Results({
            @Result(property = "nickName", column = "nick_name"),
            // 关联科室：column是SQL别名，对应dep.department_id，参数自动转为Long（MyBatis兼容）
            @Result(property = "department", column = "department_departmentId",
                    one = @One(select = "com.Cychat.doctor.mapper.DepartmentMapper.selectById")),
            // 关联医院：同理，参数自动转为Long
            @Result(property = "hospital", column = "hospital_hospitalId",
                    one = @One(select = "com.Cychat.doctor.mapper.HospitalMapper.selectById"))
    })
    List<DoctorVo> getAllDoctors();

    /**
     * 分页查询医生（联查科室、医院、用户昵称，支持筛选）
     */
    @Select("<script>" +
            "SELECT d.*, " +
            "dep.department_id as department_departmentId, " +
            "h.hospital_id as hospital_hospitalId, " +
            "u.nick_name as nick_name " +
            "FROM doctor d " +
            "LEFT JOIN department dep ON d.department_id = dep.department_id " +
            "LEFT JOIN hospital h ON dep.hospital_id = h.hospital_id " +
            "LEFT JOIN user_info u ON d.userinfo_id = u.userinfo_id " +
            "<where>" +
            "<if test='hospitalId != null'>AND h.hospital_id = #{hospitalId}</if>" +
            "<if test='departmentId != null'>AND d.department_id = #{departmentId}</if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "nickName", column = "nick_name"),
            @Result(property = "department", column = "department_departmentId",
                    one = @One(select = "com.Cychat.doctor.mapper.DepartmentMapper.selectById")),
            @Result(property = "hospital", column = "hospital_hospitalId",
                    one = @One(select = "com.Cychat.doctor.mapper.HospitalMapper.selectById"))
    })
    IPage<DoctorVo> selectDoctorVoPage(
            IPage<Doctor> page,
            @Param("hospitalId") Integer hospitalId,  // 前端传递Integer，SQL自动转Long
            @Param("departmentId") Integer departmentId);

    /**
     * 根据userinfo_id查询当前登录医生
     */
    @Select("SELECT d.*, " +
            "dep.department_id as department_departmentId, " +
            "h.hospital_id as hospital_hospitalId, " +
            "u.nick_name as nick_name " +
            "FROM doctor d " +
            "LEFT JOIN department dep ON d.department_id = dep.department_id " +
            "LEFT JOIN hospital h ON dep.hospital_id = h.hospital_id " +
            "LEFT JOIN user_info u ON d.userinfo_id = u.userinfo_id " +
            "WHERE d.userinfo_id = #{userinfoId}")
    @Results({
            @Result(property = "nickName", column = "nick_name"),
            @Result(property = "department", column = "department_departmentId",
                    one = @One(select = "com.Cychat.doctor.mapper.DepartmentMapper.selectById")),
            @Result(property = "hospital", column = "hospital_hospitalId",
                    one = @One(select = "com.Cychat.doctor.mapper.HospitalMapper.selectById"))
    })
    DoctorVo selectDoctorVoByUserInfoId(@Param("userinfoId") Integer userinfoId);

    /**
     * 根据doctorId查询医生详情
     */
    @Select("SELECT d.*, " +
            "dep.department_id as department_departmentId, " +
            "h.hospital_id as hospital_hospitalId, " +
            "u.nick_name as nick_name " +
            "FROM doctor d " +
            "LEFT JOIN department dep ON d.department_id = dep.department_id " +
            "LEFT JOIN hospital h ON dep.hospital_id = h.hospital_id " +
            "LEFT JOIN user_info u ON d.userinfo_id = u.userinfo_id " +
            "WHERE d.doctor_id = #{doctorId}")
    @Results({
            @Result(property = "nickName", column = "nick_name"),
            @Result(property = "department", column = "department_departmentId",
                    one = @One(select = "com.Cychat.doctor.mapper.DepartmentMapper.selectById")),
            @Result(property = "hospital", column = "hospital_hospitalId",
                    one = @One(select = "com.Cychat.doctor.mapper.HospitalMapper.selectById"))
    })
    DoctorVo selectDoctorVoById(@Param("doctorId") Integer doctorId);
}